package com.andendginegame;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.util.Log;

public class Hole extends Sprite{

	private String id = "";
	public final static int HOLE_WIDTH = 100;
	public final static int HOLE_HEIGHT = 200;
	private TextureRegion imgOff = null;
	private TextureRegion imgOn = null;
	public static final int CLOSE = 0;
	public static final int OPEN = 1;
	public static final int LOST_CLOSE = 2;
	
	
	
	private int status = 0;
	public Hole(float x,float y, int status, TextureRegion imgOff, TextureRegion imgOn) {
		super(x, y, imgOff);
		this.status = status;
		this. imgOff = imgOff;
		this. imgOn = imgOn;
		
		id = ""+x+"-"+y;
	}
	public Hole(float x, float y, int status, TextureRegion imgOff,
			TextureRegion imgOn, Scene scene) {
		super(x, y, imgOff);
		this.status = status;
		this. imgOff = imgOff;
		this. imgOn = imgOn;
		
		id = ""+x+"-"+y;
		
		scene.registerTouchArea(this);
		scene.getTopLayer().addEntity(this);
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		switch(pSceneTouchEvent.getAction()) {
			case TouchEvent.ACTION_DOWN:
				this.setScale(1.25f);
				this.status = CLOSE;
				if(GameManager.getInstance().isMultiTouchSupported() &&
						this.status == LOST_CLOSE) {
					GameManager.lessNumLostClose();
					
//					super.getTextureRegion().
					this.setScale(1.0f);
				}
				GameManager.getInstance().addHolePressed();
				Log.d("AndEngineGame", "STATUS: CLOSE");
				break;
			case TouchEvent.ACTION_MOVE:
//				if(this.mGrabbed) {
//					this.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
//				}
				break;
			case TouchEvent.ACTION_UP:
				if(GameManager.getInstance().isMultiTouchSupported() &&
						this.status == CLOSE) {
					this.status = LOST_CLOSE;
					GameManager.addNumLostClose();
					this.setScale(1.0f);
				}
				break;
		}
		return true;
	}
	
	
	
	public TextureRegion getImgOff() {
		return imgOff;
	}
	public void setImgOff(TextureRegion imgOff) {
		this.imgOff = imgOff;
	}
	public TextureRegion getImgOn() {
		return imgOn;
	}
	public void setImgOn(TextureRegion imgOn) {
		this.imgOn = imgOn;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	
	
	
	public boolean equals(Hole h) {
		return this.getId().equals(h.getId()) || (this.getX() == h.getX() && this.getY() == h.getY());
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	

}
